package com.cn.mmd.controller;

import com.cn.mmd.model.*;
import com.cn.mmd.service.CourseService;
import com.cn.mmd.service.CourseWeeklyService;
import com.cn.mmd.service.StudentGroupingService;
import com.cn.mmd.service.TeacherDataLService;
import com.cn.mmd.tools.SplitSpring;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 分组
 */

@Controller
@RequestMapping(value = "page/grouping")
public class GroupingController {

    @Autowired
    private CourseService courseService;

    @Autowired
    private StudentGroupingService studentGroupingService;

    @Autowired
    private TeacherDataLService teacherDataLService;

    @Autowired
    private TokenController tokenController;
    /**
     * 添加组名 关联 教师id 课程id
     *
     * @return
     */
    @RequestMapping(value = "/add")
    public synchronized
    @ResponseBody
    Object GroupingStudent(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        String teacherId = request.getParameter("teacherId"); //"10001197";
        String courseId = request.getParameter("courseId"); //"01541001";
        String groupingName = request.getParameter("groupingName"); //"哈";
        String token= request.getParameter("token");
        TeacherData teacherData;
        teacherData = (TeacherData) tokenController.token(teacherId,token);
        if (teacherData!=null) {
            Grouping grouping;
            grouping = new Grouping(teacherId, courseId, groupingName);
            if (studentGroupingService.insertSelective(grouping) > 0) {
                map.put("code", 200);
                map.put("message", "成功");
            } else {
                map.put("code", 201);
                map.put("message", "添加失败");
            }
        }else {
            map.put("code", 202);
            map.put("message", "此用户没有权限做此操作");
        }
        return map;
    }

    /**
     * 修改学生小组
     *
     * @return
     */
    @RequestMapping(value = "/updStu")
    public synchronized
    @ResponseBody
    Object GroupingUpdStu(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> map1 = new HashMap<String, Object>();
        String student = request.getParameter("student");
        String teacherId = request.getParameter("teacherId");
        String token= request.getParameter("token");
        TeacherData teacherData;
        teacherData = (TeacherData) tokenController.token(teacherId,token);
        if (teacherData!=null) {
            JSONArray jsonArray = JSONArray.fromObject(student);
            JSONObject jsonObject;
            int groupingId;
            int groupId;
            if (jsonArray != null && jsonArray.size() > 0) {
                for (int i = 0; i < jsonArray.size(); i++) {
                    jsonObject = jsonArray.getJSONObject(i);
                    groupingId = Integer.parseInt(jsonObject.getString("groupingId"));
                    groupId = Integer.parseInt(jsonObject.getString("groupId"));
                    StudentGrouping studentGrouping = new StudentGrouping(groupId, groupingId);
                    map1.put(i + "", studentGrouping);
                }
                if (map1 != null && map1.size() == jsonArray.size()) {
                    Map.Entry entry;
                    Iterator iters = map1.entrySet().iterator();
                    for (int j = 0; j < map1.size(); j++) {
                        entry = (Map.Entry) iters.next();
                        StudentGrouping studentGrouping = (StudentGrouping) entry.getValue();
                        studentGroupingService.updateByPrimaryKeySelective(studentGrouping);
                    }
                    map.put("code", 200);
                    map.put("message", "成功");
                } else {
                    map.put("code", 201);
                    map.put("message", "修改失败");
                }
            } else {
                map.put("code", 202);
                map.put("message", "传值失败");
            }
        }else {
            map.put("code", 203);
            map.put("message", "此用户没有权限做此操作");
        }
        return map;
    }

    /**
     * 修改组名 关联修改学生小组名
     *
     * @return
     */
    @RequestMapping(value = "/upd")
    public synchronized
    @ResponseBody
    Object GroupingUpd(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        List<StudentGrouping> studentGroupingList;
        Integer groupingName = Integer.parseInt(request.getParameter("groupingId"));
        StudentGrouping studentGrouping;
        String groupingName1 = request.getParameter("groupingNameUpd"); //"哈哈"
        String teacherId = request.getParameter("teacherId");
        String token= request.getParameter("token");
        TeacherData teacherData;
        teacherData = (TeacherData) tokenController.token(teacherId,token);
        if (teacherData!=null) {
            if (!groupingName1.equals("未分组")) {
                Grouping grouping = new Grouping();
                grouping.setGroupnameid(groupingName);
                grouping = studentGroupingService.selectByAll(grouping);
                if (grouping != null) {
                    grouping = new Grouping(groupingName1, grouping.getGroupnameid());
                    studentGrouping = new StudentGrouping(groupingName);
                    if (studentGroupingService.updateByPrimaryKeySelective(grouping) > 0) {
                        studentGroupingList = studentGroupingService.selectByTeaIdCouIDGroName(studentGrouping);
                        if (studentGroupingList != null) {
                            for (int i = 0; i < studentGroupingList.size(); i++) {
                                studentGrouping = new StudentGrouping(studentGroupingList.get(i).getGroupid(), grouping.getGroupnameid());
                                studentGroupingService.updateByPrimaryKeySelective(studentGrouping);
                            }
                            map.put("message", "成功");
                            map.put("code", 200);
                        }
                    } else {
                        map.put("message", "修改失败");
                        map.put("code", 201);
                    }
                } else {
                    map.put("message", "未知错误没有小组原来的名字");
                    map.put("code", 203);
                }
            } else {
                map.put("message", "命名不正确");
                map.put("code", 204);
            }
        }else {
            map.put("message", "此用户没有权限做此操作");
            map.put("code", 205);
        }
        return map;
    }

    /**
     * 删除小组关联删除小组
     *
     * @return
     */
    @RequestMapping(value = "/del")
    public synchronized
    @ResponseBody
    Object GroupingDel(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        Integer groupingName = Integer.parseInt(request.getParameter("groupingId"));
        String teacherId = request.getParameter("teacherId");
        String token= request.getParameter("token");
        TeacherData teacherData;
        teacherData = (TeacherData) tokenController.token(teacherId,token);
        if (teacherData!=null) {
            List<StudentGrouping> studentGroupingList;
            Grouping grouping = new Grouping();
            grouping.setGroupnameid(groupingName);
            StudentGrouping studentGrouping = new StudentGrouping(groupingName);
            if (studentGroupingService.deleteByAll(grouping) > 0) {
                studentGroupingList = studentGroupingService.selectByTeaIdCouIDGroName(studentGrouping);
                if (studentGroupingList != null) {
                    for (int i = 0; i < studentGroupingList.size(); i++) {
                        studentGrouping = new StudentGrouping(studentGroupingList.get(i).getGroupid(), 0);
                        studentGroupingService.updateByPrimaryKeySelective(studentGrouping);
                    }
                    map.put("message", "成功");
                    map.put("code", 200);
                } else {
                    map.put("message", "数据获取失败");
                    map.put("code", 201);
                }
            } else {
                map.put("message", "此小组不存在");
                map.put("code", 202);
            }
        }else {
            map.put("message", "此用户没有权限做此操作");
            map.put("code", 203);
        }
        return map;
    }

    /**
     * 分组前显示课程的列表
     *
     * @return
     */
    @RequestMapping(value = "/course")
    public synchronized
    @ResponseBody
    Object GroupingCourse(HttpServletRequest request) {
        Map<Object, Object> map1 = new HashMap<Object, Object>();
        List<Map> mapList = new ArrayList<Map>();
        String teacherId = request.getParameter("teacherId");
        String token= request.getParameter("token");
        TeacherData teacherData;
        teacherData = (TeacherData) tokenController.token(teacherId,token);
        if (teacherData!=null) {
            String teacherName = teacherData.getTeachername();
            String id;
            Map<String, Object> map;
            int i = 0;
            List<Course> list = courseService.selectByTeacher(teacherName);
            if (list != null && list.size() > 0) {
                for (; i < list.size(); i++) {
                    map = new HashMap<String, Object>();
                    id = list.get(i).getCourseid();
                    map.put("courseName", list.get(i).getCoursename());
                    map.put("courseId", id);
                    map.put("courseClassRoom", list.get(i).getCourseclassroom());
                    map.put("courseClassS", list.get(i).getCourseclasss());
                    mapList.add(map);
                }
                map1.put("code", 200);
                map1.put("message", "成功");
                map1.put("data", mapList);
            } else {
                map1.put("code", 201);
                map1.put("message", "当前老师没有课程");
                map1.put("data", mapList);
            }
        }else {
            map1.put("code", 203);
            map1.put("message", "此用户没有权限做此操作");
        }
        return map1;
    }
}

